package cn.ly.security.controller;


import cn.ly.security.entity.Equipment;
import cn.ly.security.entity.Info;
import cn.ly.security.service.EquipmentService;
import cn.ly.security.service.impl.EquipmentServiceImpl;
import cn.ly.security.service.impl.InfoServiceImpl;
import cn.ly.security.util.DataPojo;
import cn.ly.security.util.Pagely;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Info")
@CrossOrigin("*")
@Api(tags="设备管理")
public class InfoController {
    @Autowired
    InfoServiceImpl infoService;
    @Autowired
    EquipmentServiceImpl equipmentService;
    @GetMapping("/selectAllDevice")
    public DataPojo selectAllDevice(Pagely pagely){
        Page<Info> infoPage = new Page<>(pagely.getCurrent(), pagely.getSize());
        int count = infoService.count();
        List<Info> infos = infoService.selectAllDevice((pagely.getCurrent() - 1) * pagely.getSize(), pagely.getSize());
        infoPage.setTotal(count);
        infoPage.setRecords(infos);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(infoPage);
        return new DataPojo("200","success",objects);
    }
    @PostMapping("/updateDevice")
    public DataPojo updateDevice(Info info, Equipment equipment){
        String iName = info.getIName();
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("e_cate",equipment.getECate());
        Equipment one = equipmentService.getOne(queryWrapper);
        Integer eId = one.getEId();
        info.setIDevicecateId(eId);
        boolean save = infoService.save(info);
        ArrayList<Object> arrayList = new ArrayList<>();
        arrayList.add(save);
        return new DataPojo("200","success",arrayList);
    }
    @PostMapping("/updateRepairStage")
    public  DataPojo updateRepairStage(Integer iId){
        Info info = new Info();
        info.setIId(iId);
        info.setIRepairStage("维修中");
        boolean save = infoService.updateById(info);
        ArrayList<Object> arrayList = new ArrayList<>();
            arrayList.add(save);
            return new DataPojo("200","success",arrayList);
    }
    @GetMapping("/selectDeviceByName")
    public DataPojo selectDeviceByName(@RequestParam("iName") String iName, @RequestParam("current")Integer current,@RequestParam("size")Integer size){
        List<Info> info = infoService.selectDeviceByName(iName,current,size);
        Page<Info> infoPage=new Page<>(current,size);
        infoPage.setRecords(info);
        ArrayList<Object> objects=new ArrayList<>();
        objects.add(infoPage);
        return new DataPojo("200","success",objects);
    }

    @PutMapping("/insertDevice")
    public DataPojo insertDevice(Info info,Equipment equipment){
        Info info1 = new Info();
        QueryWrapper<Equipment> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("e_cate",equipment.getECate());
        Equipment one = equipmentService.getOne(queryWrapper);
        Integer eId = one.getEId();
        info1.setIName( info.getIName());
        info1.setIStage(info.getIStage());
        info1.setIDevicecateId(eId);
        boolean save = infoService.save(info1);
        ArrayList<Object> objects=new ArrayList<>();
        objects.add(save);
        return  new DataPojo("200","success",objects);
    }

    @DeleteMapping("/deleteByPrimaryKey")
    public boolean deleteByPrimaryKey(Integer id) {
        boolean i= infoService.removeById(id);
        return i;
    }

   @DeleteMapping("/deleteAllById")
    public  DataPojo deleteAllById(Integer[] ids){
       ArrayList<Object> arrayList = new ArrayList<>();
       boolean b=false;
       for (Integer id:ids) {
            b = infoService.removeById(id);
           if (b==false){
             return new DataPojo("500","del err",arrayList);
           }
       }
       arrayList.add(b);
        return new DataPojo("200","success",arrayList);
   }
    @GetMapping("/selectAreaByName")
    public  DataPojo selectAreaByName(String iArea){
        List<Info> infos = infoService.selectAreaByName(iArea);
        ArrayList<Object> objects =new ArrayList<>();
        objects.add(infos);
        return  new DataPojo("200","",objects);


    }

    @GetMapping("/selectAllByArea")
    public DataPojo selectAllByArea(){
        List<Info> infos = infoService.selectAllByArea();
        ArrayList<Object> objects =new ArrayList<>();
        objects.add(infos);
        return  new DataPojo("200","",objects);
    }


}
